#include "dlg_bookgetandset.h"
#include "ui_dlg_bookgetandset.h"

Dlg_BookGetAndSet::Dlg_BookGetAndSet(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dlg_BookGetAndSet)
{
    ui->setupUi(this);
}

Dlg_BookGetAndSet::~Dlg_BookGetAndSet()
{
    delete ui;
}

void Dlg_BookGetAndSet::setType(bool isReturn,QString Bookname)
{
    m_isReturn=isReturn;
    if(!m_isReturn)
        ui->btn_get->setText("归还");
    ui->lb_Bookname->setText(Bookname);
}

void Dlg_BookGetAndSet::setBookId(int id)
{
    m_bookid=id;
}

void Dlg_BookGetAndSet::on_btn_get_clicked()
{
    do{
        auto strName=ui->le_username->text();
        auto strPassword=ui->le_password->text();
        int userid=0;
        auto ret=Sqlmgr::getinstance()->login(strName,strPassword,userid);
        if(!ret)
        {
            this->done(0);
            break;
        }
        if(m_isReturn)
        {
        Sqlmgr::getinstance()->borrowBook(QString::number(userid),QString::number(m_bookid));
        }
        else
        {
            Sqlmgr::getinstance()->returnBook(QString::number(userid),QString::number(m_bookid));
        }

    }while(false);
    //判断用户名和密码是否正确

    //图书库存-1

    //生成一条借阅记录
    this->done(1);//这里的1和0也就是该页面消失后的返回值,返回到void Cell_Bookmgr::on_btn_borrow_clicked()的ret中
}


void Dlg_BookGetAndSet::on_btn_cancel_clicked()
{
    this->done(0);
    //this->hide();
}

